var User = (function() {

	function User() {
		
	}

	User.prototype.getModuleName = function() {
		return "等级";
	}
	
	User.prototype.Messager_Title = function(){
		return '温馨提示';
	}
	
	User.prototype.Data_Id = function (row){
		return {'id':row['id']};
	}

	User.prototype.init = function() {
		$user.grid
				.datagrid({
					url : arguments[0],
					rownumbers : true,
					lines : true,
					fit:true,
					fitColumns : true,
					singleSelect:true,
					method:'get',
					onSelect : $user.rowClick,
					checkbox:true,
					columns : [ [ {
						field : 'id',
						title : 'ID编号',
						hidden : true,
						width : 100
					}, {
						field : 'username',
						sortable:true,
						title : '名称',
						width : 100
					}, {
						field : 'department',
						title : '部门',
						sortable:true,
						width : 100,
						formatter:function(val,row){
							if(val){
								return val.name;
							}
						}
					}, {
						field : 'createTime',
						title : '创建时间',
						sortable:true,
						width : 100,
						formatter:function(val,row){
							return new Date(val).toLocaleString();
						}
					}, {
						field : 'status',
						title : '状态',
						sortable:true,
						formatter : function(val, row) {
							if (val == 'THAW') {
								return '正常';
							} else {
								return '<font style="color:red">冻结</font>';
							}
						},
						width : 100
					} ] ]
				});
		if($user.toolbars.length==0){//使用默认配置
			var $group_buttons = $('.north-menu .button-group button');
			$user.toolbars = [$group_buttons.eq(1),$group_buttons.eq(2),$group_buttons.eq(3),$group_buttons.eq(4),$group_buttons.eq(5)];
		}
		
	}
	
	User.prototype.rowClick = function() {
		var row = $user.grid.datagrid('getSelected');
		if(!row)return;
		if (row.status == 'THAW') {
			$user.toolbars[0].attr('disabled',false);
			$user.toolbars[1].attr('disabled',false);
			$user.toolbars[2].attr('disabled',false);
			$user.toolbars[3].attr('disabled',false);
			$user.toolbars[4].attr('disabled',true);
		} else {
			$user.toolbars[1].attr('disabled',true);
			$user.toolbars[2].attr('disabled',true);
			$user.toolbars[3].attr('disabled',true);
			$user.toolbars[4].attr('disabled',false);
		}
	}

	User.prototype.del = function() {
		var row = $user.grid.datagrid('getSelected');
		if (row) {
			$user.progress();
			$.post($user.delUrl, $user.Data_Id(row) , function(data) {
				$user.operatorData(data,$('.move_right:eq(2)'));
			});
		}
	}
	
	User.prototype.operatorData = function(data,el){
		if(!$.isJson(data)){
			data = data.toJSON();
		}
		if(data.success){
			$user.progress('close');
		}else{
			$user.progress('error');
			el.find('.alert').remove();
			var error_list = $('<div class="alert alert-red" style="display:none;"><span class="close rotate-hover" onclick="$(this).parent().fadeOut(function(){$(this).remove();});"></span></div>').appendTo(el).fadeIn();
			if(data.msgs.length>0){
				for(msg in data.msgs){
					$('<p style="margin-bottom:0px;">'+(parseInt(msg)+1)+'. '+data.msgs[msg]+'</p>').appendTo(error_list);
				}
			}else{
				$('<p style="margin-bottom:0px;">'+data.msg+'</p>').appendTo(error_list);
			}
		}
	}
	
	User.prototype.gridload = function(){
		$user.grid.datagrid('reload');
	}

	User.prototype.add = function() {
		$user.button = true;
		$($user.department).combotree('destroy').remove();
		$($user.role).combotree('destroy').remove();
		
		var department = addDepartment();// 重新添加combotree
		var role = addRole();// 重新添加combotree
		$("#password").textbox({
			required:true
		});
		if(_self_time==null){
			_self_time = setTimeout(function(){
				clearTimeout(_self_time);
				_self_time = null;
				if(_self_time==null){
					if($('.north-menu .left .active').attr('clear')=='true'){
						$user.form.form('clear');
					}
				}
			},1200);
		}
		
	}
	
	var _self_time = null;

	User.prototype.edit = function() {
		$user.button = false;
		var node = $user.grid.datagrid('getSelected');
		$.extend(node,{ids:[]});
		
		$($user.department).combotree('destroy').remove();
		$($user.role).combotree('destroy').remove();
		
		var department = addDepartment();// 重新添加combotree
		
		$("#password").textbox({
			required:false
		});
		if(_self_time==null){
			_self_time = setTimeout(function(){
				clearTimeout(_self_time);
				_self_time = null;
				if(_self_time==null){
					if($('.north-menu .left .active').attr('clear')=='true'){
						$user.form.form('clear');
						$user.form.form('load',node);
						department.combotree('setValue',node.department.id);
						addRole(node);// 重新添加combotree
					}
				}
			},1200);
		}
	}

	User.prototype.congeal = function() {// 冻结
		var row = $user.grid.datagrid('getSelected');
		$user.progress();
		$.post($user.congealUrl, $user.Data_Id(row) , function(data) {
			$user.operatorData(data,$('.move_right:eq(3)'));
		});
	}

	User.prototype.thaw = function($self) {// 解冻
		
		var row = $user.grid.treegrid('getSelected');
		$user.progress();
		$.post($user.thawUrl, $user.Data_Id(row) , function(data) {
			$user.operatorData(data,$('.move_right:eq(0)'));
		});
	}

	User.prototype.initButtons = function() {
		$('#sudtm-menu-edit').linkbutton('disable');
		$('#sudtm-menu-remove').linkbutton('disable');
	}

	User.prototype.formSubmit = function(url,successInfo,title) {
		$user.form.form('submit', {
			url : url,
			onSubmit : function() {
				var isValid = $(this).form('validate');
				if (isValid) {
					$user.progress();
				}
				return $(this).form('validate');
			},
			success : function(data) {
				$user.operatorData(data,$('.move_right:eq(0)'));
			}
		});
	}

	User.prototype.save = function() {
		if ($user.button) {
			$user.formSubmit($user.addUrl,'添加');// 表单添加
		} else {
			$user.formSubmit($user.editUrl,'编辑');// 表单编辑
		}
	}

	User.prototype.close = function(url) {
		dialogClose();
	}
	
	User.prototype.searchClear = function(el) {
		var from = $(el).parents('form');
		from.form('clear');
	}
	
	User.prototype.search = function() {
		var form = $(arguments[0]).parents('form');
		var arr_list = [];
		var inputs = form.find("input");
		inputs.each(function(){
			var name = $(this).attr('name');
			if(name){
				var arr_name = name.split(':');
				if(!(arr_name[1]=='between'&&arr_name[3]=='end')&&!(arr_name.slice(-1)=='notnull'&&''==$(this).val())){
					var _ar = [];
					_ar.push('{"searchName":');
					_ar.push('"'+arr_name[1]+'"');
					_ar.push(',');
					_ar.push('"searchField":[');
					_ar.push('"'+arr_name[0]+'"');
					_ar.push(',');
					_ar.push('"'+arr_name[2]+'"');
					_ar.push(',');
					if(arr_name[1]=="like"&&arr_name[3].slice(1,-1).split(',')[0]=='%'){
						_ar.push('"%');
						_ar.push($(this).val());
					}else if(arr_name[1]=="between"&&arr_name[3]=='begin'){
						_ar.push('"'+$(this).val()+'"');
						_ar.push(',');
						_ar.push('"'+form.find('input[name*="'+arr_name.slice(0,3).join(':')+':end"]').val()+'"');
					}else{
						_ar.push('"'+$(this).val()+'"');
					}
					if(arr_name[1]=="like"&&arr_name[3].slice(1,-1).split(',')[1]=='%'){
						_ar.push('%"');
					}
					_ar.push(']}');
					arr_list.push(_ar.join(''));
				}
			}
		});
		$user.grid.datagrid('load',{'searchs':'['+arr_list.join()+']'});
	}
	
	User.prototype.showOrHiddenSearchBox = function(el) {
		var self = $(el);
		var searchBox = $('div.search-box');
		if(searchBox.is(':visible')){
			searchBox.each(function(){
				$(this).dequeue();
			}).fadeOut();
		}else{
			searchBox.each(function(){
				$(this).dequeue();
			}).fadeIn();
		}
	}
	
	User.prototype.progress = function(status){
		if(status==undefined){
			$('.b-r-loading img:eq(0)').fadeIn();
			$('.b-r-loading img:eq(1)').hide();
		}else if(status=='close'){
			$('.b-r-loading img:eq(0)').hide();
			$('.b-r-loading img:eq(1)').fadeIn();
			var bl = $('.b-r-loading div').show().find('b').text(3);
			var count = 3;
			var bi=null;
			try{
				clearInterval(bi);
			}catch(e){}
			var bi = setInterval(function(){
				count--;
				bl.text(count);
				if(count<=0){
					operator_item($('.north-menu .left .button-group button:first')[0]);
					$('.b-r-loading div').hide();
					$('.b-r-loading img').hide();
					clearInterval(bi);
				}
			},1000);
		}else if('error'){
			$('.b-r-loading img:eq(0)').hide();
			$('.b-r-loading img:eq(1)').hide();
			$('.b-r-loading img:eq(2)').fadeIn();
		}
		
	}
	
	function addDepartment() {
		var department = $(
				'<table data-options="lines:true" name="department.id" id="department" style="width:200px;"></table>')
				.appendTo($user.departmentBox);
		department.combotree({
					url : arguments.length == 1 ? $user.departmentTreeUrl : $user.departmentTreeUrl,
					valueField : 'id',
					textField : 'name',
					editable : false,
					required : true,
					method:'get',
					onBeforeExpand : function(node) {
						department.combotree('tree').tree('options').url = $user.departmentTreeUrl
								+ '?id=' + node.id;
					}
				});
		return department;
	}
	
	function addRole() {
		var role = $(
				'<table data-options="lines:true" multiple name="ids" id="role" style="width:200px;"></table>')
				.appendTo($user.roleBox);
		role.combotree({
					url : arguments.length == 1 ? ($user.check_by_user_url+'?id='+arguments[0].id+'&username='+arguments[0].name) : $user.roleTreeUrl,
					valueField : 'id',
					textField : 'name',
					editable : false,
					method:'get',
					onBeforeExpand : function(node) {
						role.combotree('tree').tree('options').url = $user.roleTreeUrl
								+ '?id=' + node.id;
					}
				});
		return role;
	}

	return User;
})();
